package top.heue.epro.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.NumberFormat;
import android.graphics.Bitmap;

public class WindowUtil {
    public static Boolean isShowing = false;
    private View view;
    private final Context context;
    WindowManager.LayoutParams layoutParams;
    private final WindowManager windowManager;
    private final View prepareView;
    public WindowUtil(Context context, String src, int alpha) {
        this.context = context;
        layoutParamsInit();
        //布局建立
        //ImageView img = new ImageView(context);
        //img.setLayoutParams(layoutParams);
        //img.setAlpha(0.5f);
        //img.setImageBitmap(BitmapFactory.decodeStream(getInputStreamFromPath("/storage/emulated/0/Pictures/高清缩小版.png")));
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(4);
        float i = (float)alpha / 255;
        /*
        String floatAlpha = nf.format(0.3);
        */
        //LOG.i("透明度结果", String.valueOf(i));
        prepareView = getFinalView(src);
        assert prepareView != null;
        prepareView.setAlpha(i);
        windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    }
    
    public void show() {
        if (windowManager != null) {
            //LOG.i("显示悬浮窗");
            windowManager.addView(prepareView, layoutParams);
            this.view = prepareView;
            isShowing = true;
        }
    }
    
    /*
    public void show(View view) {
        if (windowManager != null) {
            Log.i("显示悬浮窗");
            windowManager.addView(view, layoutParams);
            this.view = view;
            isShowing = true;
        }
    }
    */

    public void close() {
        //LOG.i("关闭悬浮窗");
        windowManager.removeView(view);
        isShowing = false;
    }

    /*public void reShow() {
        LOG.i("显示悬浮窗");
        windowManager.addView(view, layoutParams);
        isShowing = true;
    }*/

    private void layoutParamsInit() {
        layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //26及以上必须使用TYPE_APPLICATION_OVERLAY   @deprecated TYPE_PHONE
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        layoutParams.gravity = Gravity.START | Gravity.TOP;
        layoutParams.format = PixelFormat.TRANSPARENT;
        /*
         layoutParams.x = 0;
         layoutParams.y = 0;
         */
    }
    
    private View getFinalView(String src) {
        if(src.startsWith("/")) {//是路径
            ImageView view = new ImageView(context);
            Bitmap bit = NormalUtil.getFitImage(context, getInputStreamFromPath(src));
            view.setImageBitmap(bit);
            return view;
        }
        if(src.startsWith("#")) {//是颜色
            View view = new View(context);
            view.setBackgroundColor(Color.parseColor(src));
            return view;
        }
        //LOG.i("不太可能的异常，用户可能修改过配置");
        return null;
    }

    private InputStream getInputStreamFromPath(String filePath) {
        FileInputStream fileInputStream = null;
        try {
            File file = new File(filePath); 
            fileInputStream = new FileInputStream(file);
        } catch(Exception e) {
            //LOG.e("获取输入流异常", e);
        }
        return fileInputStream;
    }
}
